package com.yhd.tomcat;

import com.yhd.tomcat.handler.config.ConfigHandler;
import com.yhd.tomcat.handler.connection.ConnectionHandler;
import com.yhd.tomcat.handler.event.EventHandler;
import com.yhd.tomcat.handler.process.ProcessHandler;
import com.yhd.tomcat.listener.EventListener;

import java.net.ServerSocket;

/**
 * @author yhd
 * @email yinhuidong1@xiaomi.com
 * @description TODO
 * @since 2021/6/10 下午3:07
 */
public class Bootstrap {




    public static void main(String[] args) {

        BootstrapBuilder.build().init();
    }

}
class BootstrapBuilder{
    private static volatile BootstrapBuilder bootstrapBuilder;
    private ServerSocket serverSocket;

    private ConfigHandler configHandler;

    private ConnectionHandler connectionHandler;

    private ProcessHandler processHandler;

    private EventHandler eventHandler;

    public static BootstrapBuilder build() {
        if (bootstrapBuilder==null){
            synchronized (Object.class){
                if (bootstrapBuilder==null){
                    bootstrapBuilder=new BootstrapBuilder();
                }
            }
        }
        return bootstrapBuilder;
    }

    public void init() {
        //0.开启监听请求事件线程
        listener();
        //1.解析加载配置文件
        configHandler = ConfigHandler.getSingleton();
        //2.创建连接
        connectionHandler = ConnectionHandler.getSingleton();
        serverSocket = connectionHandler.getServerSocket();
        //3.请求处理器
        processHandler = ProcessHandler.getSingleton();

        processHandler.process(serverSocket);

    }

    private void listener() {
        System.out.println("Bootstrap--->事件开始监听");
        eventHandler = EventHandler.getSingleton();
        new Thread(() -> eventHandler.processEvent()).start();
    }
}